package com.snts.synchronization.schema.extensions;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.snts.synchronization.schema.SchemaUpdate;

public class ExtensionsGenerator {

	private static final Log log = LogFactory.getLog(ExtensionsGenerator.class);
	
	private static final String[] extensions = new String[]{ "get_trans_id.sql", "get_value.sql", "set_value.sql", "get_rest_id.sql", "set_rest_id.sql" };
	
	public List<SchemaUpdate> generateExtensions(){
		List<SchemaUpdate> extensions = new ArrayList<SchemaUpdate>(ExtensionsGenerator.extensions.length);
		for(String extension : ExtensionsGenerator.extensions){
			try {
				SchemaUpdate schemaUpdate = new SchemaUpdate(IOUtils.toString(getClass().getResource(extension)));
				schemaUpdate.setName("extension - " + extension.substring(0, extension.lastIndexOf('.')));
				extensions.add(schemaUpdate);
			} catch (NullPointerException npe){
				log.warn("Failed to load extension from resource " + getClass().getPackage().getName() + "." + extension, npe);
			} catch (IOException e) {
				System.out.println("Failed to load extension from resource " + getClass().getPackage().getName() + "." + extension);
				e.printStackTrace();
			}
		}
		return extensions;
	}

}
